<script lang="ts" setup name="XtxCity">
import { ref, watch, watchEffect } from 'vue'
import { onClickOutside } from '@vueuse/core'
import { AddressType, AreaType } from './city'
import axios from 'axios'
defineProps({
  address: {
    type: String,
    default: '',
  },
})
const emits = defineEmits<{
  (e: 'changeAddress', address: AddressType): void
}>()
// 控制城市选择菜单的显示与隐藏的布尔值
const active = ref(false)
const target = ref(null)
onClickOutside(target, () => {
  // 只要点击了city组件之外就会触发这个回调函数
  active.value = false
})

const areaList = ref<AreaType[]>([])
const catchList = ref<AreaType[]>([]) // 备份的数据
// 获取省市区数据的方法
const getAreaList = async () => {
  const res = await axios.get<AreaType[]>(
    'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
  )
  areaList.value = res.data
  catchList.value = res.data
}
getAreaList()

const selectAddress = ref({
  // 省份的信息
  provinceName: '',
  provinceCode: '',
  // 市的信息
  cityName: '',
  cityCode: '',
  // 区/县的信息
  countyName: '',
  countyCode: '',
})

// 点击选择省市区/县
const handleClick = (item: AreaType) => {
  // 省份信息的收集
  if (item.level === 0) {
    selectAddress.value.provinceName = item.name
    selectAddress.value.provinceCode = item.code
  }
  // 市信息的收集
  if (item.level === 1) {
    selectAddress.value.cityName = item.name
    selectAddress.value.cityCode = item.code
  }
  // 将点击的省/市的 arealist 赋值给v-for循环的数组
  // 如果点到最后一层(选择完了区了就直接隐藏下拉菜单)
  if (item.level === 2) {
    selectAddress.value.countyName = item.name
    selectAddress.value.countyCode = item.code
    active.value = false
    // 子传父发布自定义事件, 将收集到的地址信息传递给父组件
    emits('changeAddress', selectAddress.value)
  }
  areaList.value = item.areaList
}
// 只要下拉菜单隐藏 保证每次打开的下拉菜单直接将备份的数据赋值给areaList
/* watch(
  () => active.value,
  (newVal) => {
    // console.log(newVal)
    if (!newVal) {
      areaList.value = catchList.value
    }
  }
) */
watchEffect(() => {
  if (!active.value) {
    areaList.value = catchList.value
  }
})
</script>
<template>
  <div class="xtx-city" ref="target">
    <div class="select" :class="{ active }" @click="active = !active">
      <span class="placeholder" v-if="!address">请选择配送地址</span>
      <span class="value" v-else>{{ address }}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show="active">
      <span
        class="ellipsis"
        v-for="item in areaList"
        :key="item.code"
        @click="handleClick(item)"
        >{{ item.name }}</span
      >
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
